package com.geotmt.billingcenter.modules.cost.service.impl;

import com.geotmt.billingcenter.common.entry.CostBaseEntry;
import com.geotmt.billingcenter.common.enumpackage.CostFeeFlagEnum;
import com.geotmt.billingcenter.common.enumpackage.RuleLabelEnum;
import com.geotmt.billingcenter.common.vo.consume.ConsumeVo;
import com.geotmt.billingcenter.common.vo.cost.CostHisVo;
import com.geotmt.billingcenter.common.vo.cost.CostVo;
import com.geotmt.billingcenter.common.vo.params.ParamsCost;
import com.geotmt.billingcenter.common.vo.params.ParamsForDel;
import com.geotmt.billingcenter.modules.cost.mapper.CostBaseMapper;
import com.geotmt.billingcenter.modules.cost.service.CostBaseService;
import com.geotmt.billingcenter.modules.rules.service.RulesService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 类描述
 * @Author      yanghanwei
 * @Mail        yanghanwei@geotmt.com
 * @Date        2019/3/19 18:55
 * @Version     v1
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class CostBaseServiceImpl implements CostBaseService {

    private static final Logger logger = LoggerFactory.getLogger(CostBaseServiceImpl.class);

    @Resource
    private CostBaseMapper costBaseMapper;

    @Resource
    private RulesService rulesService;

    @Override
    public Integer addBase(CostBaseEntry costBase) {
        logger.info("成本 addBase Service");
        costBaseMapper.addBase(costBase);
        return costBase.getId();
    }

    @Override
    public int updateBase(CostVo consumeVo) {
        logger.info("成本 updateBase Service");
        return costBaseMapper.updateBase(consumeVo);
    }

    @Override
    public int deleteBase(ParamsForDel obj) {
        logger.info("成本 deleteBase Service");
        return costBaseMapper.deleteBase(obj);
    }

    @Override
    public List<CostVo> listCurr(ParamsCost obj) {
        logger.info("成本 list Service");
        Integer pageNum = obj.getPageNum();
        Integer pageSize = obj.getPageSize();
        String orderBy = obj.getOrderBy();
        if(null != pageNum && null != pageSize){
            PageHelper.startPage(pageNum,pageSize);
        }
        if(!com.alibaba.druid.util.StringUtils.isEmpty(orderBy)){
            PageHelper.orderBy(orderBy);
        }
        List<CostVo> costVos = costBaseMapper.listCurr(obj);
        if(null != costVos){
            for (CostVo costVo : costVos){
                if(CostFeeFlagEnum.STAIR.value.equals(costVo.getFeeFlag())){
                    costVo.setStairRuleList(rulesService.stairListGetByRid(costVo.getRId(),RuleLabelEnum.COST_CURR.value));
                }
            }
        }
        return costVos;
    }

    @Override
    public List<CostVo> listAll(ParamsCost obj) {
        String orderBy = obj.getOrderBy();
        if(!StringUtils.isEmpty(orderBy)){
            PageHelper.orderBy(orderBy);
        }
        List<CostVo> costVos = costBaseMapper.listAll(obj);
        //查询 规则的详细计费规则
        if(null != costVos){
            for (CostVo costVo : costVos){
                if(CostFeeFlagEnum.STAIR.value.equals(costVo.getFeeFlag())){
                    costVo.setStairRuleList(rulesService.stairListGetByRid(costVo.getRId(),RuleLabelEnum.COST_CURR.value));
                }
            }
        }
        return costVos;
    }

    @Override
    public CostVo currGetById(Integer id) {
        logger.info("成本 getById Service");
        CostVo costVo = costBaseMapper.currGetById(id);
        if(CostFeeFlagEnum.STAIR.value.equals(costVo.getFeeFlag())){
            costVo.setStairRuleList(rulesService.stairListGetByRid(costVo.getRId(), RuleLabelEnum.COST_CURR.value));
        }
        return costVo;
    }

    @Override
    public CostHisVo getCost(int dsId, int outInterfaceId, String dateDay) {
        logger.info("获去有效规则 Service");
        CostHisVo cost = costBaseMapper.getCost(dsId, outInterfaceId, dateDay);
        if(null == cost){
            return null;
        }
        //sign 是在上班查询的时候 sql 拼接进去的
        if(CostFeeFlagEnum.STAIR.value.equals(cost.getFeeFlag())){
            Integer ruleLabel = null;
            if("curr".equals(cost.getSign())){
                ruleLabel = RuleLabelEnum.COST_CURR.value;
            }else if("his".equals(cost.getSign())){
                ruleLabel = RuleLabelEnum.COST_HIS.value;
            }
            cost.setStairRuleList(rulesService.stairListGetByRid(cost.getRId(), ruleLabel));
        }
        return cost;
    }
}
